package com.webull.information.center.carwler.task;

import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.task.handler.StockReuterEnNewsHandler;
import com.webull.securities.client.model.Stock2Information;
import com.webull.securities.client.model.parm.TickerProfileParm;
import com.webull.securities.client.service.StockService;

@Component
public class News_ReutersEnStok_Task {
	protected final Logger logger = LogManager.getLogger(getClass());

	private @Autowired StockService stockService;
	private @Autowired StockReuterEnNewsHandler stockReuterEnNewsHandler;
	private @Autowired @Qualifier("executor_news") ThreadPoolTaskExecutor threadPoolTaskExecutor;
	private final Integer pageSize = 100;

	/**
	 * 路透
	 * 
	 * @param regionId
	 * @param lastId
	 * @param loop
	 * @return
	 * @since 0.1.0
	 */
	public Integer fullReutersEnStockNews(Integer regionId, Integer lastId, Integer loop) {

		logger.info(
				"News_ReutersEnStok_Task.News_ReutersEnStok_TaskExchangeCode start exchangeCode:{},lastId:{},loop:{}",
				regionId, lastId, loop);
		int loop_ = 0;
		full: do {
			if (loop != null && loop > 0 && ++loop_ > loop) {
				break full;
			}
			TickerProfileParm parm = new TickerProfileParm();
			parm.setLastId(lastId);
			parm.setRegionId(regionId);
			parm.setQueryNum(pageSize);
			List<Stock2Information> res = stockService.queryPagingInform(parm);
			if (res == null || res.isEmpty()) {
				lastId = null;
				break full;
			}
			logger.info("News_ReutersEnStok_Task.News_ReutersEnStok_TaskExchangeCode stocks:" + res.size());
			lastId = res.get(res.size() - 1).getTickerId();
			procss(res);
		} while (true);

		return lastId;
	}

	private void procss(List<Stock2Information> res) {
		try {
			if (res == null || res.isEmpty())
				return;
			final CountDownLatch latch = new CountDownLatch(res.size());
			AtomicInteger at = new AtomicInteger(0);
			for (Stock2Information s : res) {
				StockTitle stockTitle = new StockTitle();
				BeanUtils.copyProperties(s, stockTitle);
				threadPoolTaskExecutor.execute(new Runnable() {
					@Override
					public void run() {
						try {
							stockReuterEnNewsHandler.handlerByStockTitle4Reuters(stockTitle);
							logger.info("News_ReutersEnStok_Task.procss stockTitle:" + stockTitle);
						} finally {
							latch.countDown();
						}
					}
				});

				if (at.incrementAndGet() % 20 == 0) {
					Random random = new Random();
					try {
						Thread.sleep(random.nextInt(1000) * 10);
					} catch (Exception e) {
					}
				}
			}
			latch.await();
			logger.info("News_ReutersEnStok_Task procss end::::" + res.size());
		} catch (Exception e) {
			logger.error("News_ReutersEnStok_Task 全量新闻任务失败", e);
		}
	}

}
